package edu.jptest.serviceImp;

import java.util.Date;

import edu.jptest.bean.Group;
import edu.jptest.bean.Rank;
import edu.jptest.bean.RegUser;
import edu.jptest.bean.User;
import edu.jptest.dao.GroupDAO;
import edu.jptest.dao.RankDAO;
import edu.jptest.dao.UserDAO;
import edu.jptest.util.AuthImg;
import edu.jptest.util.MD5Encoding;
import edu.jptest.util.MailUtil;

public class UserService {

	// 插入用户(注册时的信息插入数据库，只包括用户的部分信息，其它为默认值)
	public boolean insertUser(String nickname, String password, String email)
			throws Exception {
		RegUser user = new RegUser();
		UserDAO userdao = new UserDAO();
		user.setNickname(nickname);
		System.out.println("昵称： " + nickname);
		user.setPassword(MD5Encoding.getMD5ofStr(password));
		user.setEmail(email);
		user.setRegTime(new Date());
		String validateCode = MD5Encoding.getMD5ofStr((new Date()) + "");
		user.setValidate(validateCode);
		MailUtil mailUtil = new MailUtil();
		mailUtil.validate(email, password, validateCode);
		return userdao.insertUser(user);
	}

	// 判断用户名是否重复
	public boolean UserNameOk(String username) {
		UserDAO userdao = new UserDAO();
		if (userdao.getUsreIdByUserName(username) == 0) {
			return true;
		} else
			System.out.println("userId: "
					+ userdao.getUsreIdByUserName(username));
		return false;
	}

	// 判断用户的激活信息是否正确
	public boolean regValidate(String email, String validateCode) {
		UserDAO userdao = new UserDAO();
		return userdao.regValidate(email, validateCode);
	}

	// 激活用户，注册用户转正式用户
	public boolean activateRegUser(String email) {
		User user = new User();
		RegUser reguser = new RegUser();
		UserDAO userdao = new UserDAO();
		reguser = userdao.findByEmail(email);
		user.setUserName("保密");
		user.setUserNickname(reguser.getNickname());
		user.setUserEmail(reguser.getEmail());
		user.setUserPassword(reguser.getPassword());
		user.setRegTime(reguser.getRegTime());
		Rank rank = new Rank();
		RankDAO rankDAO = new RankDAO();
		rank = rankDAO.getRankById(1);
		user.setRank(rank);
		user.setUserScore(0);
		user.setUserSex("保密");
		GroupDAO groupDAO = new GroupDAO();
		Group group = new Group();
		group = groupDAO.getGroupById(1);
		user.setGroup(group);
		user.setUserIco("defaultHead.jpeg");
		user.setUserType(0);
		return userdao.activateRegUser(user);
	}

	// 发送邮件确认用户丢失密码 发送激活链接
	public void lostPassConfirm(String email, String validateCode)
			throws Exception {
		try {
			MailUtil mailutil = new MailUtil();
			mailutil.lostEmailConfirm(email, validateCode);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("邮件通知密码更改出错");
		}
	}

	// 激活密码验证激活信息
	public boolean userValidate(String email, String validateCode) {
		UserDAO userDAO = new UserDAO();
		if (userDAO.userValidate(email, validateCode) != 0) {
			return true;
		} else {
			return false;
		}
	}

	// 发邮件激活密码
	public void lostPassActivation(String email) throws Exception {
		try {
			AuthImg authImg = new AuthImg();
			String password = "";
			for (int i = 0; i < 6; i++)
				password += authImg.getRandomChar();
			User user = new User();
			UserDAO userDAO = new UserDAO();
			user = userDAO.getUserByUserId(userDAO.getUserIdByEmail(email));
			user.setUserPassword(MD5Encoding.getMD5ofStr(password));
			System.out.println("用户信息:" + user.getUserPassword()
					+ user.getUserName());
			userDAO.updateUser(user);
			MailUtil mailUtil = new MailUtil();
			mailUtil.lostPasswordNotify(email, password);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("随机生成密码发送错误。");
		}
	}

	// 验证用户登录信息
	public int loginValidate(User user) {
		UserDAO userDAO = new UserDAO();
		return userDAO.loginValidate(user);
	}

	// 通过邮箱查找用户ID
	public int getUserIdByEmail(String email) {
		UserDAO userDAO = new UserDAO();
		return userDAO.getUserIdByEmail(email);
	}

	// 通过用户ID查找用户信息
	public User getUserByUserId(int user_id) {
		UserDAO userDAO = new UserDAO();
		return userDAO.getUserByUserId(user_id);
	}

	// 修改个人信息
	public boolean modifyUser(User user) {
		UserDAO userDAO = new UserDAO();
		return userDAO.updateUser(user);
	}
}
